package com.miku.blog.utils;

import com.miku.blog.domain.pojo.SystemConstance;
import com.miku.blog.eume.HttpStatusEnum;
import com.miku.blog.exception.SystemException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author shkstart
 * @create ${}YEAR-05-29 17:37
 */
public class FilePathUtils {

    public static String generateUploadFilePath(String fileName,String start) {

        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String format = simpleDateFormat.format(date);
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String suffixType = generateSuffixType(fileName);

        String path = new StringBuilder().append(start)
                .append("/")
                .append(format)
                .append("/mikuBlog_")
                .append(uuid)
                .append(suffixType)
                .toString();

        return path;
    }

    private static String generateSuffixType(String fileName) {
        int lastIndexOf = fileName.lastIndexOf(".");
        return fileName.substring(lastIndexOf);
    }


    public static void checkImageFileSuffixType(String imageName) {
        String suffixType = generateSuffixType(imageName);
        boolean flag = true;
        for (String format : SystemConstance.SUPPORTED_IMAGE_FILE_FORMATS) {
            if (format.equalsIgnoreCase(suffixType)){
                flag = false;
                break;
            }
        }
        if (flag){
            throw new SystemException(HttpStatusEnum.UPLOAD_FILE_TYPE_ERROR);
        }
    }

}
